package cn.yuhe.org.controller;

import cn.yuhe.basic.annotation.PreAuthorize;
import cn.yuhe.basic.utils.AjaxResult;
import cn.yuhe.basic.utils.PageList;
import cn.yuhe.org.domain.Classes;
import cn.yuhe.org.query.ClassesQuery;
import cn.yuhe.org.service.IClassesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/classes")
public class ClassesController {
    @Autowired
    public IClassesService classesService;


    /**
     * 保存和修改公用的
     * @param classes  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "保存和修改对象",sn = "classes:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Classes classes){
        try {
            if( classes.getId()!=null)
                classesService.update(classes);
            else
                classesService.save(classes);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除对象信息",sn = "classes:addOrUpdate")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            classesService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "获取对象信息",sn = "classes:get")
    public Classes get(@PathVariable("id")Long id)
    {
        return classesService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name = "获取所有对象信息",sn = "classes:list")
    public List<Classes> list(){

        return classesService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @PreAuthorize(name = "分页查询数据对象信息",sn = "classes:json")
    public PageList<Classes> json(@RequestBody ClassesQuery query)
    {
        return classesService.queryData(query);
    }

    @PatchMapping
    @PreAuthorize(name = "批量删除对象信息",sn = "classes:batchDel")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        System.out.println("获取的ids="+ids);
        try {
            classesService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

}
